import os
import sys
import subprocess

# 确保在正确的目录中
os.chdir(os.path.dirname(os.path.abspath(__file__)))

# 运行命令在conda环境中启动服务
try:
    # 使用conda run命令在指定环境中执行命令
    cmd = [
        'conda', 'run', '-n', 'text3.9',
        'python', '-m', 'flask', 'run', 
        '--host=0.0.0.0', '--port=5000'
    ]
    
    print(f"启动命令: {' '.join(cmd)}")
    
    # 设置环境变量
    env = os.environ.copy()
    env['FLASK_APP'] = 'app.py'
    env['FLASK_ENV'] = 'development'
    
    # 执行命令
    subprocess.run(cmd, env=env, check=True)
    
except subprocess.CalledProcessError as e:
    print(f"命令执行失败: {e}")
    sys.exit(1)
except Exception as e:
    print(f"发生错误: {e}")
    sys.exit(1)